# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：deal_image.py
#日期：2019-10-19
#备注：对图片换底色
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from PIL import Image,ImageEnhance
from pycacho.cachobase.file_deal import an_save_txt
from pycacho.cachobase.logger import Logger
logger = Logger("deal_image").get_log()


# 对图片进行处理
def deal_image(infile,outfile):
    im = Image.open(infile)
    enhancer = ImageEnhance.Contrast(im)
    im = enhancer.enhance(2)
    im = im.convert('1')
    data = im.getdata()
    w, h = im.size
    black_point = 0
    for x in range(1, w - 1):
        for y in range(1, h - 1):
            mid_pixel = data[w * y + x]  # 中央像素点像素值
            if mid_pixel == 0:  # 找出上下左右四个方向像素点像素值
                top_pixel = data[w * (y - 1) + x]
                left_pixel = data[w * y + (x - 1)]
                down_pixel = data[w * (y + 1) + x]
                right_pixel = data[w * y + (x + 1)]

                # 判断上下左右的黑色像素点总个数
                if top_pixel == 0:
                    black_point += 1
                if left_pixel == 0:
                    black_point += 1
                if down_pixel == 0:
                    black_point += 1
                if right_pixel == 0:
                    black_point += 1
                if black_point >= 3:
                    im.putpixel((x, y), 0)
                # print black_point
                black_point = 0
    im.show()
    im.save(outfile)


def get_pix(infile):
    img = Image.open(infile)
    w, h = img.size
    #print(w,h)
    #an_save_txt('./images/id.txt',str(w)+'--'+str(h))
    for i in range(w):
        for j in range(h):
            L = img.getpixel((i, j))
            if L[0]>80 and L[1]>80 and L[2]>80 and i>120 and i<191 and j>0 and j<50:
                print(L)
                img.putpixel((i, j), (255, 0, 0,255))
            #an_save_txt('./images/id.txt', str(i+1) + '--' + str(j+1) + '--' + str(L))
    img.save("./images/id_1.png", "PNG")
    img.show()


def transparent(infile,type):
    img = Image.open(infile)
    new_png = img.convert('RGBA')
    L, H = new_png.size         #图片尺寸
    color_0 = new_png.getpixel((0, 0))   # 返回图片某个坐标点颜色。
    logger.info('返回图片某个坐标点颜色%s', color_0)
    for h in range(H):
        for l in range(L):
            dot = (l, h)
            color_1 = new_png.getpixel(dot)
            if type == 'change':
                if 80 < color_1[2] < 180:
                    print(h,l,color_1)
                    new_png.putpixel(dot, color_1)
                else:
                    new_png.putpixel(dot, (255,255,255,0))
            else:
                pass
    new_png.save("./file/a-copy2.png", "PNG")
    #new_png.show()


# 合并图片 file:上面图片，background：背景图片
def merge_image(background,file):
    img = Image.open(background).convert('RGBA')
    out = Image.open(file).convert('RGBA')
    img.paste(out,(500,500))
    img.save('./file/out.png','PNG')


if __name__ == '__main__':
    #get_pix('./images/id_1.png')
    transparent('./file/a-copy1.png','change')
    #transparent('./file/a.jpg','')
    #merge_image('./file/a-copy.png', './file/c-copy.png')